package com.oktest.measure.media.video

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView

class CircleBackgroundTextView : AppCompatTextView {
    private var paint: Paint
    private var rectF: RectF
    private val circleColor = -0xff0100

    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    init{
        gravity = Gravity.CENTER
        paint = Paint()
        paint.apply {
            strokeWidth = DEFAULT_STROKE_WIDTH.toFloat()
            color = circleColor
            style = Paint.Style.STROKE
            isAntiAlias = true
        }
        rectF = RectF()
    }

    fun setCircleColor(circleColor: Int) {
        paint.color = circleColor
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        rectF.set(
            DEFAULT_STROKE_WIDTH / 2f,
                DEFAULT_STROKE_WIDTH / 2f,
                width - DEFAULT_STROKE_WIDTH / 2f,
                height - DEFAULT_STROKE_WIDTH / 2f)
        canvas.drawOval(rectF, paint)
    }

    companion object {
        private const val DEFAULT_STROKE_WIDTH = 2
    }

    fun startRecordingAnim() {
        val set = AnimatorSet()
        set.playTogether(
                ObjectAnimator.ofFloat(this, "scaleX", *floatArrayOf(1.0f, 1.2f, 1.5f)),
                ObjectAnimator.ofFloat(this, "scaleY", *floatArrayOf(1.0f, 1.2f, 1.5f)),
                ObjectAnimator.ofFloat(this, "alpha", *floatArrayOf(1.0f, 0.25f, 0.0f))
        )
        set.setDuration(300L).start()
    }

    fun stopRecordingAnim() {
        val set = AnimatorSet()
        set.playTogether(
                ObjectAnimator.ofFloat(this, "scaleX", *floatArrayOf(1.5f, 1.2f, 1.0f)),
                ObjectAnimator.ofFloat(this, "scaleY", *floatArrayOf(1.5f, 1.2f, 1.0f)),
                ObjectAnimator.ofFloat(this, "alpha", *floatArrayOf(0.0f, 0.25f, 1.0f))
        )
        set.setDuration(300L).start()
    }

}